local CopyL = require("copy.CopyL")
local CopyDefine = require("copy.Define")
local ObjHuman = require("core.ObjHuman")
local ExcelMap = require("excel.Map")
local CCJJWarConfig = require("excel.CCJJWar")
local Timer = require("core.Timer")
local Map = require("core.Map")
local Msg = require("core.Msg")
local Obj = require("core.Obj")
local Lang = require("common.Lang")
local DailyActFL = require("activity.DailyActFL")
local ActDefine = require("activity.Define")
local MapScan = require("core.MapScan")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local Grid = require("bag.Grid")
local Util = require("common.Util")
local RoleHandler = require("role.HandlerL")
local ExcelLevelRewardConf = require("excel.LevelReward")
local Buffer = require("buffer.Buffer")
local Broadcast = require("broadcast.Broadcast")
local HtmlUtil = require("common.HtmlUtil")
local MailLogicFL = require("mail.MailLogicFL")
local SceneEvent = require("core.SceneEvent")
local Config = require("Config")
local CopyConfig = ExcelMap.copy
local CCJJWarBaseConfig = CCJJWarConfig.Base[1]
local CCJJWarRewardConfig = CCJJWarConfig.Reward[1]
local CCJJWarIncomeConfig = CCJJWarConfig.Income[1]

----------------------------
----------------------------
--V1 援军值 V2 威望 V3 积分
----------------------------
----------------------------

local NONE_SIDE = 0             --暂无分队
local RED_SIDE = 1              --红队
local BLUE_SIDE = 2             --蓝队

CCJJWAR_STATUS_LEAVED = -1        --离开战场
CCJJWAR_STATUS_NORMAL = 0         --正常状态
CCJJWAR_STATUS_DIE = 1            --已经死亡

HumanWarDataList = HumanWarDataList or {}
SkillData = SkillData or {}
StatusList = StatusList or {}
RankList = RankList or {}
KillList = KillList or {}
HeartBeatCount = HeartBeatCount or {}

function checkEnter(human)
    local nStartTime, nEndTime = getActTime()
    if nStartTime > 0 and nEndTime > 0 then
        if human.db.ccjjwarEnterTime >= nStartTime and human.db.ccjjwarEnterTime <= nEndTime then
            Broadcast.sendBroadcast(human, Lang.DOWN, Lang.CCJJWAR_ALD_ENTER)  --只能进一次
            return false
        end

        local nStartSplit =  os.time() - nStartTime
        if nStartSplit >= CCJJWarBaseConfig.stopEnterTime then
            Broadcast.sendBroadcast(human, Lang.DOWN, Lang.CCJJWAR_STOP_ENTER)  --停止进入
            return false
        end
    else
        Broadcast.sendBroadcast(human, Lang.DOWN, Lang.CCJJWAR_NOT_START)  --活动暂未开启
        return false
    end
    return true
end

function onGetHumanStatus(humanUuid, roomID)
    return HumanWarDataList[roomID]._humanDataList[humanUuid] and HumanWarDataList[roomID]._humanDataList[humanUuid].status or CCJJWAR_STATUS_LEAVED
end

function onCCJJWarRoomBegin(copyID, roomID)
    HumanWarDataList[roomID] = {}
    HumanWarDataList[roomID]._humanDataList = {}
    HumanWarDataList[roomID].static = -1    -- -1 备战状态 0 战斗状态 1 结束状态
    StatusList[roomID] = {}
    RankList[roomID] = {}
    KillList[roomID] = {}
    SkillData[roomID] = {}

    HeartBeatCount[roomID] = 0
end

--得到开始时间
function getActTime()
    local nActData = getActData() 
    if not nActData or nActData.status ~= ActDefine.ACT_STATE_RUNNING then
        return 0, 0
    end
    return nActData.startTime, nActData.endTime
end

--得到活动剩余时间
function getLeftTime()
    local nActData = getActData()
    if not nActData or nActData.status ~= ActDefine.ACT_STATE_RUNNING then
        return 0
    end
    local t =  nActData.endTime - os.time()
    if t < 0 then t = 0 end
    return t
end

function getActData()
    return DailyActFL.onGetActData(ActDefine.ACT_TYPE_CCJJ)
end

--开始副本
--function onCCJJWarHumanBeginCopy(human, copyID, roomID)
--
--end

function onCCJJWarHumanEndCopy(human, copyID, roomID)
    onHumanCtl(human, roomID, copyID)
end

function onCCJJWarHumanEnterCopy(human, copyID, roomID)
    if not HumanWarDataList[roomID]._humanDataList[human._id] then
        HumanWarDataList[roomID]._humanDataList[human._id] = {v3 = 0, v3old = 0, status = CCJJWAR_STATUS_NORMAL, beHitList = {}}
        human.enterCopyTime = Timer.now
        local nStartTime, nEndTime = getActTime()
        if nStartTime > 0 and nEndTime > 0 then
            local nStartSplit =  os.time() - nStartTime
            if nStartSplit < CCJJWarBaseConfig.beginfight then
                local mm = Msg.gc.GC_SHOW_TXT
                mm.txt = string.format("草船借箭将在%d秒后开始，请做好战斗准备！", CCJJWarBaseConfig.beginfight - nStartSplit)
                Msg.send(mm, human.fd)
            end
        end
    else
        HumanWarDataList[roomID]._humanDataList[human._id].status = DHFIGHT_STATUS_NORMAL
    end

    local nCopyConfig = CopyConfig[copyID]
    if human.db.map_id == nCopyConfig.mapList[2] then
        print("sdflkjslfkjskdfjsdlkfjkl")
        onV3Buf(human, HumanWarDataList[roomID]._humanDataList[human._id].v3, nil, nil, true)
        onJumpBackPos(human)
        for k, v in ipairs(CCJJWarBaseConfig.enterAddBuf) do
            Buffer.add(v, human, human)
        end
    end
    onSendConfigData(human, copyID)
    human.db.ccjjwarEnterTime = os.time()
end

--function onCCJJWarHumanLeaveCopy(human, copyID, roomID, sceneID)
--end

local function get_grid(itemID, itemNum, itemColor)
    local grid = Grid.create(nil, itemID, itemNum, nil, ItemDefine.qualityOut[itemColor])
    return grid
end

function onSendConfigData(human, copyID)
    local nSendMsg = Msg.gc.GC_CCJJWAR_CONFIGDATA
    nSendMsg.killRankReward[0] = 0
    nSendMsg.beginfighttime = 0 

    local nStartTime, nEndTime = getActTime()
    if nStartTime > 0 and nEndTime > 0 then
        local nStartSplit =  os.time() - nStartTime
        if nStartSplit < CCJJWarBaseConfig.beginfight then
            nSendMsg.beginfighttime = CCJJWarBaseConfig.beginfight - nStartSplit
        end
    end

    --得到草船借箭杀敌排名奖励
    local nKillRankReward = CCJJWarRewardConfig.killRankReward
    for k, v in ipairs(nKillRankReward) do
        nSendMsg.killRankReward[0] = nSendMsg.killRankReward[0] + 1
        local nKillRankRewardMsgData = nSendMsg.killRankReward[nSendMsg.killRankReward[0]]
        nKillRankRewardMsgData.reward[0] = 0
        for kk, vv in ipairs(v) do
            nKillRankRewardMsgData.reward[0] = nKillRankRewardMsgData.reward[0] + 1
            local nRewarData = nKillRankRewardMsgData.reward[nKillRankRewardMsgData.reward[0]]
            local grid = get_grid(vv[1], vv[2], vv[3])
            Grid.makeItem(grid, nRewarData)
        end
    end
    Msg.send(nSendMsg, human.fd)
end

function onSendCopyData(copyID, roomID, sceneID)
    local nSendMsg = Msg.gc.GC_CCJJWAR_COPYDATA
    nSendMsg.leftTime = getLeftTime()
    nSendMsg.rankList[0] = 0
    for k, v in ipairs(RankList[roomID]) do
        nSendMsg.rankList[0] = nSendMsg.rankList[0] + 1
        local nRankMsgData = nSendMsg.rankList[nSendMsg.rankList[0]]
        if nRankMsgData then
            nRankMsgData.name = v[1]
            nRankMsgData.score = v[2]
            nRankMsgData.killnum = KillList[roomID][v[3]] and KillList[roomID][v[3]].killcount or 0
        end
        if k >= 5 then
            break
        end
    end
    nSendMsg.myScore = 0
    nSendMsg.myKillNum = 0

    local nFds = Obj.scenes_fds[sceneID]
    if nFds then
        for k, v in pairs(nFds) do
            if v.obj_type == Obj.TYPE_HUMAN then
                nSendMsg.myScore = HumanWarDataList[roomID]._humanDataList[v._id] and HumanWarDataList[roomID]._humanDataList[v._id].v3 or 0
                nSendMsg.myKillNum = KillList[roomID][v._id] and KillList[roomID][v._id].killcount or 0
                Msg.send(nSendMsg, k)
                --Util.printTable(nSendMsg.rankList)
            end
        end
    end
end

function onCCJJWarOneSec(copyID, roomID)
    HeartBeatCount[roomID] = HeartBeatCount[roomID] + 1
    local nCopyConfig = CopyConfig[copyID]

    local nMapID = nCopyConfig.mapList[1]
    local nStartSplit = 0
    -- 战斗开启时间到
    local nStartTime, nEndTime = getActTime()
    if nStartTime > 0 and nEndTime > 0 then
        nStartSplit =  os.time() - nStartTime
        if HumanWarDataList[roomID].static == -1 and nStartSplit >= CCJJWarBaseConfig.beginfight then
            HumanWarDataList[roomID].static = 0 
        end
    end

    if HumanWarDataList[roomID].static == 0 then
        nMapID = nCopyConfig.mapList[2]

        local nFightAddExpTime = HeartBeatCount[roomID] % CCJJWarBaseConfig.fightAddExpTime == 0 and true or false
        if nFightAddExpTime then
            local nHumanList = CopyL.getCopyHumanList(copyID, roomID)
            for k, v in pairs(nHumanList) do
                if nFightAddExpTime and v.db.map_id == nCopyConfig.mapList[2] then
                    ObjHuman.addExp(v, ExcelLevelRewardConf[8][v.db.lv].exp)
                end
            end
        end

        -- 剑雨技能施放
        local nSceneID = CopyL.copyList[copyID][roomID][nMapID]
        if nSceneID and nSceneID > 0 then
            local nSkillBegin = false
            if CCJJWarBaseConfig.skill1Data[nStartSplit] then
                SceneEvent.doSceneEvent(nSceneID, CCJJWarBaseConfig.skill1Data[nStartSplit][1])
                table.insert(SkillData[roomID], {os.time(), CCJJWarBaseConfig.skill1Data[nStartSplit][2], CCJJWarBaseConfig.skill1Data[nStartSplit][3]})
                nSkillBegin = true
            end

            if not nSkillBegin and SkillData and SkillData[roomID] then
                for k, v in ipairs(SkillData[roomID]) do
                    if os.time() - v[1] <= v[2] then
                        local nSum, nHumanList = MapScan.scanAreaLiveHuman(nMapID, nSceneID, CCJJWarBaseConfig.skill1AreaID)
                        if nSum > 0 then
                            for nHumanUuid in pairs(nHumanList) do
                                local nHuman = ObjHuman.onlineUuid[nHumanUuid]
                                if nHuman then
                                    onAddV3(v[3], nHuman)
                                end
                            end
                        end
                    end
                end
            end
        end

        local nRemainTime = getLeftTime()
        if nRemainTime / 60 == 5 then
            onSendBroadcast(Lang.CCJJWAR_ENDBEFORE_5MIN, Lang.UP, copyID, roomID)
        end
    end

    --集体复活
    if HeartBeatCount[roomID] % CCJJWarBaseConfig.humanReliveTime == 0 then
        local humanList = CopyL.getCopyHumanList(copyID, roomID)
        for k, v in pairs(humanList) do
            if v.db.hp <= 0 then
                local nTempCopyID = CopyL.isCopyBySceneID(v.scene_id)
                if nTempCopyID == copyID then
                    RoleHandler.CG_REVIVE_GO_HOME(v, true)
                end
            end
        end
    end

    --发送副本数据面板数据
    if HeartBeatCount[roomID] % 3 == 0 then
        local nMapID = nCopyConfig.mapList[1]
        local nSceneID = CopyL.copyList[copyID][roomID][nMapID]
        onSendCopyData(copyID, roomID, nSceneID)

        nMapID = nCopyConfig.mapList[2]
        nSceneID = CopyL.copyList[copyID][roomID][nMapID]
        onSendCopyData(copyID, roomID, nSceneID)
    end
end

function onGetReviveTime(humanUuid)
    local nRoomID = CopyL.copyHuman2Room[humanUuid]
    if HeartBeatCount[nRoomID] then
        if HumanWarDataList[nRoomID].static == 1 then
            return 0
        else
            local nLeftTime = HeartBeatCount[nRoomID] % CCJJWarBaseConfig.humanReliveTime
            return CCJJWarBaseConfig.humanReliveTime - (nLeftTime == 0 and CCJJWarBaseConfig.humanReliveTime or nLeftTime)
        end
    end
end

function onJumpBackPos(human, isDead)
    local nRoomID = CopyL.copyHuman2Room[human._id]
    local nAreaID = CCJJWarBaseConfig.bornAreaID
    if nAreaID then
        local nX, nY = Map.getPositionInArea(human.scene_id, nAreaID, true)
        Obj.jump(human, nX, nY, Msg.gc.GC_JUMP)
        onV3Buf(human, HumanWarDataList[nRoomID]._humanDataList[human._id].v3, HumanWarDataList[nRoomID]._humanDataList[human._id].v3old)
    end
end

function onCCJJWarMonsterDie(killer, monster, monsterID, copyID, roomID)
    local nHumanHurtList = monster.enemy_list
    if not nHumanHurtList then
        return
    end
    local nMonster1Data = CCJJWarBaseConfig.monster1Data[monsterID]

    if nMonster1Data then
        local nRankList = {}
        for k, v in pairs(nHumanHurtList) do
            v.humanUuid = k
            table.insert(nRankList, v)
        end

        table.sort(nRankList, function(a, b)
            return a.hurt > b.hurt
        end)

        -- 伤害排名
        for k, v in ipairs(nRankList) do
            local nHuman = ObjHuman.onlineUuid[v.humanUuid]
            if nHuman then
                if k == 1 then
                    onAddV3(CCJJWarIncomeConfig.killMonster1No1AddV3, nHuman)
                else
                    onAddV3(CCJJWarIncomeConfig.killMonster1AddV3, nHuman)
                end
            end
        end

        -- 最后一击
        onAddV3(CCJJWarIncomeConfig.killMonster1LastHitAddV3, killer)
        onSendBroadcast(string.format(Lang.CCJJWAR_KILL_MONSTER1, HtmlUtil.font(killer.name, "#00FF00")), Lang.UP, copyID, roomID)
    end
end

function onCheckComboKill()
    local nSendMsg = Msg.gc.GC_CCJJWAR_COMBOKILL
    for nRoomID, nRoomKillData  in pairs(KillList) do
        for nHumanUuid, nKillData in pairs(nRoomKillData) do
            if nKillData.change == true then
                nKillData.change = false
                nSendMsg.killNum = nKillData.comboKillCount
                local nHuman = ObjHuman.onlineUuid[nHumanUuid]
                if nHuman then
                    Msg.send(nSendMsg, nHuman.fd)
                end
            end
        end
    end
end

--人物死亡
function onCCJJWarRoleDieCB(atker, human, copyID, roomID)
    if atker.obj_type ~= Obj.TYPE_HUMAN then
        return
    end
    local nKillData = KillList[roomID][atker._id]
    if nKillData then
        local nKillDiffTime = os.time() - nKillData.comboKillTime
        if nKillDiffTime <= CCJJWarBaseConfig.comboKillTime then
            nKillData.comboKillCount = nKillData.comboKillCount + 1
        else
            nKillData.comboKillCount = 1
        end
        nKillData.killcount = nKillData.killcount + 1
        nKillData.noDieComboKillCount = nKillData.noDieComboKillCount + 1
        nKillData.comboKillTime = os.time()
        nKillData.killUuid = human._id
        nKillData.change = true
    else
        KillList[roomID][atker._id] = {comboKillTime = os.time(), killUuid = human._id, killcount = 1, comboKillCount = 1, noDieComboKillCount = 1, change = true}
        nKillData = KillList[roomID][atker._id]
    end

    local nAlgAdd = false
    for i = 1, 6 do
        local nRankData = RankList[roomID][i]
        if nRankData and nRankData[3] == human._id then
            nAlgAdd = true
        elseif i == 6 then
            nAlgAdd = true
        end

        if nAlgAdd then
            local nDecV3 = math.ceil((HumanWarDataList[roomID]._humanDataList[human._id].v3 or 0) * (CCJJWarIncomeConfig.rankKillAddV3[i][1] / 100))
            onAddV3(-nDecV3, human, true)
            onAddV3(nDecV3 + CCJJWarIncomeConfig.rankKillAddV3[i][2], atker)
            local nContent = string.format(Lang.CCJJWAR_KILL_DEC,
            HtmlUtil.font(atker.name, "#00FF00"),
            HtmlUtil.font(human.name, "#00FF00"), nDecV3)
            onSendBroadcast(nContent, Lang.GROSSIP, copyID, roomID)
            break
        end
    end

    -- 击杀广播相关
    local nContent = nil
    local nNoDieComboKillCount = nKillData.noDieComboKillCount
    local nNoDieComboKillData = CCJJWarBaseConfig.noDieComboMsg[nNoDieComboKillCount]
    if (nNoDieComboKillCount - 50) % CCJJWarBaseConfig.noDieComboKillMaxNum == 0 then
        nContent = CCJJWarBaseConfig.noDieComboMsg[100]
    else
        if nNoDieComboKillData then
            nContent = string.format(nNoDieComboKillData[1], HtmlUtil.font(atker.name, "#00FF00"), HtmlUtil.font(string.format(Lang.CCJJWAR_COMBOKILL, nNoDieComboKillCount), "#F1FF0A"))
            onSendBroadcast(nContent, Lang.UP, copyID, roomID)
        end
    end

    local nBeKillData = KillList[roomID][human._id]
    if nBeKillData and nBeKillData.noDieComboKillCount >= CCJJWarBaseConfig.breakNoDieComboKill then
        nContent = string.format(Lang.CCJJWAR_BREAK_COMBOKILL,
        HtmlUtil.font(atker.name, "#00FF00"),
        HtmlUtil.font(human.name, "#00FF00"),
        HtmlUtil.font(string.format(Lang.CCJJWAR_COMBOKILL, nBeKillData.noDieComboKillCount), "#F1FF0A"))
        nBeKillData.noDieComboKillCount = 0
        onSendBroadcast(nContent, Lang.UP, copyID, roomID)
    end

    if HeartBeatCount[roomID] % CCJJWarBaseConfig.humanReliveTime == 0 then
        RoleHandler.CG_REVIVE_GO_HOME(human, true)
    end
end

function onCCJJWarRoomFinish(copyID, roomID)
    local nRoomHumanList = CopyL.getCopyHumanList(copyID, roomID)
    for k, v in ipairs(nRoomHumanList) do
        local nHuman = ObjHuman.onlineUuid[v._id]
        if nHuman then
            --死亡的进行最后复活
            if nHuman.db.hp <= 0 then
                RoleHandler.CG_REVIVE_GO_HOME(nHuman, true)
            end
            onHumanCtl(nHuman, roomID, copyID)
        end
    end
end

-- 结算奖励
function onCCJJWarRoomClear(copyID, roomID)
    HumanWarDataList[roomID] = nil
    KillList[roomID] = nil
    HeartBeatCount[roomID] = nil
    RankList[roomID] = nil
    HeartBeatCount[roomID] = nil
    SkillData[roomID] = nil
end

function onHumanCtl(human, roomID)
    HumanWarDataList[roomID]._humanDataList[human._id].status = CCJJWAR_STATUS_LEAVED
--    for k, v in ipairs(CCJJWarBaseConfig.enterAddBuf) do
--        Buffer.removeID(v, human)
--    end
    Buffer.add(31050, human, human)
end

function onCCJJWarRoomEnd()
    for nRoomID in pairs(HumanWarDataList) do
        if HumanWarDataList[nRoomID] and HumanWarDataList[nRoomID].static == 1 then
            break
        end
        HumanWarDataList[nRoomID].static = 1
        local nCCJJWarRoomData = HumanWarDataList[nRoomID]
        if nCCJJWarRoomData then
            local nCopyConfig = CopyConfig[CopyDefine.COPY_CCJJWAR_ID]
            if nCopyConfig then
                local nMapID = nCopyConfig.mapList[2]
                local nSceneID = CopyL.copyList[CopyDefine.COPY_CCJJWAR_ID][nRoomID][nMapID]
                if nSceneID then
                    onSendCopyData(CopyDefine.COPY_CCJJWAR_ID, nRoomID, nSceneID)
                end
            end

            local nRankRewardList = nil
            for k, v in ipairs(RankList[nRoomID]) do
                nRankRewardList = {}
                local nRewardMailContent = ""
                if k == 1 then
                    for k, v in ipairs(CCJJWarRewardConfig.killRankReward[1]) do
                        nRankRewardList[#nRankRewardList + 1] = get_grid(v[1], v[2], v[3])
                    end
                    nRewardMailContent = Lang.CCJJWAR_RANK_NO1
                elseif k >= 2 and k <= 3 then
                    for k, v in ipairs(CCJJWarRewardConfig.killRankReward[2]) do
                        nRankRewardList[#nRankRewardList + 1] = get_grid(v[1], v[2], v[3])
                    end
                    nRewardMailContent = Lang.CCJJWAR_RANK_NO2
                elseif k >= 4 and k <= 5 then
                    for k, v in ipairs(CCJJWarRewardConfig.killRankReward[3]) do
                        nRankRewardList[#nRankRewardList + 1] = get_grid(v[1], v[2], v[3])
                    end
                    nRewardMailContent = Lang.CCJJWAR_RANK_NO3
                else
                    for k, v in ipairs(CCJJWarRewardConfig.killRankReward[4]) do
                        nRankRewardList[#nRankRewardList + 1] = get_grid(v[1], v[2], v[3])
                    end
                    nRewardMailContent = Lang.CCJJWAR_RANK_NO4
                end

                MailLogicFL.CreateNewMailForUser(v[3], Lang.CCJJWAR_MAIL_TITLE, nRewardMailContent, nRankRewardList)
            end
        end

        CopyL.roomFinish(CopyDefine.COPY_CCJJWAR_ID, nRoomID, CopyDefine.COPY_FINISH_ACT_END, "活动结束")
    end
end

function onV3Buf(human, v3New, v3Old, die, enter)
    if not v3Old then
        v3Old = v3New
    end
    local nOldBufID = nil 
    local nNewBufID = nil 
    for k, v in ipairs(CCJJWarBaseConfig.V3AddBuf) do
        if v3Old <= v[1] and not nOldBufID then
            nOldBufID = v[2] 
        end
        if v3New <= v[1] and not nNewBufID then
            nNewBufID = v[2]
        end
    end
    if (v3New == 0 and v3Old == 0) or enter then
        Buffer.add(nNewBufID, human, human)
    elseif nOldBufID ~= nNewBufID and not die then
        Buffer.removeID(nOldBufID, human)
        Buffer.add(nNewBufID, human, human)
    end
end

-- 增加积分
function onAddV3(v3, human, die)
    local nHumanUuid = human._id
    local nRoomID = CopyL.copyHuman2Room[nHumanUuid]
    if HumanWarDataList[nRoomID] then
        local nHumanWarData = HumanWarDataList[nRoomID]._humanDataList[human._id]
        if nRoomID and nRoomID > 0 and nHumanWarData and nHumanWarData.status ~= CCJJWAR_STATUS_LEAVED then
            local nV3Src = nHumanWarData.v3
            local nV3Temp = nHumanWarData.v3 + v3
            nHumanWarData.v3old = nV3Src
            nHumanWarData.v3 = nV3Temp < 0 and 0 or nV3Temp
            onV3Buf(human, nHumanWarData.v3, nV3Src, die)
            if v3 > 0 or (v3 < 0 and nV3Src > 0) then
                onV3Rank(human, v3, nRoomID)
            end
        end
    end
end

function onCollectDieCB(human, collect, collect_id)
    local nMonster2ConfigData = CCJJWarBaseConfig.monster2Data[collect_id]
    if nMonster2ConfigData then
        onAddV3(CCJJWarIncomeConfig.killMonster2AddV3, human)
    end
end

function onV3Rank(human, score, roomID)
    local nHumanUuid = human._id
    local nSceneID = human.scene_id
    local nScoreRankData = RankList[roomID]
    if not nSceneID or nSceneID <= 0 or not RankList or not nScoreRankData then
        return
    end

    local nScore = HumanWarDataList[roomID]._humanDataList[nHumanUuid].v3

    local nBeforeRank = 0
    local nCurRank = 0

    if #nScoreRankData == 0 then
        onAddRankList(nScoreRankData, 1, human, nScore)
    else
        for k, v in ipairs(nScoreRankData) do
            if v[3] == nHumanUuid then
                nBeforeRank = k
                break
            end
        end

        if nBeforeRank == 1 and (nScore >= nScoreRankData[1][2] or #nScoreRankData == 1) then
            nScoreRankData[1][2] = nScore
            nCurRank = 1
        elseif nBeforeRank > 0 and (nScoreRankData[nBeforeRank -1] and nScore <= nScoreRankData[nBeforeRank - 1][2]) and ((nScoreRankData[nBeforeRank + 1] 
            and nScore >= nScoreRankData[nBeforeRank + 1][2]) or nBeforeRank == #nScoreRankData) then
            nScoreRankData[nBeforeRank][2] = nScore
            nCurRank = nBeforeRank
        else
            if nBeforeRank > 0 then
                table.remove(nScoreRankData, nBeforeRank)
            end
            for k = #nScoreRankData, 1, -1 do
                if nScoreRankData[k] then
                    if k == 1 then
                        if nScore < nScoreRankData[k][2] then
                            onAddRankList(nScoreRankData, k + 1, human, nScore)
                            nCurRank = k + 1
                            break
                        else
                            onAddRankList(nScoreRankData, 1, human, nScore)
                            nCurRank = 1
                            break
                        end
                    else
                        if nScore <= nScoreRankData[k][2] then
                            onAddRankList(nScoreRankData, k + 1, human, nScore)
                            nCurRank = k + 1
                            break
                        end
                    end
                end
            end

        end
    end
end

function onAddRankList(killRankData, rank, human, score)
    table.insert(killRankData, rank, {human.name, score, human._id})
end

function onSendBroadcast(content, type, copyID, roomID)
    local nMsg = Msg.gc.GC_BROADCAST
    nMsg.type = type
    nMsg.msg = content
    CopyL.sendCopy(nMsg, copyID, roomID)
end

DailyActFL.onRegisterCallBack(ActDefine.ACT_TYPE_CCJJ, nil, nil, onCCJJWarRoomEnd)

